package com.shiro.factory;

import com.entity.SysMenu;
import com.service.SysMenuService;
import com.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @author lanouhn
 */
public class FilterChainDefinitionMapBuilder {
    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private SysMenuService sysMenuService;
    private LinkedHashMap<String, String> map;

    public LinkedHashMap<String, String> buildFilterChainDefinitionMap() {

        map = new LinkedHashMap<>();
        List<SysMenu> sysMenuList = sysMenuService.selectSysMenu();
        //1.静态资源和登录都可以匿名访问
        map.put("/assets/**", "anon");
        map.put("/admin/login.jsp", "anon");
        map.put("/admin/login", "anon");
        map.put("/admin/getGifCode", "anon");
        map.put("/admin/housing-estate-add.jsp", "perms[housing-estate-list.jsp]");
        map.put("/admin/scrap-collector-add.jsp", "perms[scrap-collector-list.jsp]");
        map.put("/admin/scrap-add.jsp", "perms[scrap-list.jsp]");
        map.put("/admin/system-admin-add.jsp", "perms[system-admin-list.jsp]");
        map.put("/admin/system-category-add.jsp", "perms[system-category-list.jsp]");
        map.put("/admin/system-dict-add.jsp", "perms[system-dict-list.jsp]");
        map.put("/admin/system-menu-add.jsp", "perms[system-menu-list.jsp]");
        map.put("/admin/system-role-add.jsp", "perms[system-role-list.jsp]");
        //2.给相应的界面访问加上相对应的权限
        for (SysMenu sysMenu : sysMenuList) {
            if (!"".equals(sysMenu.getUrl()) && sysMenu.getUrl() != null) {
                String stringKye = "/admin/" + sysMenu.getUrl();
                String stringValue = "perms[" + sysMenu.getUrl() + "]";
                map.put(stringKye, stringValue);
            } else {
                continue;
            }
        }
        //剩下的界面必须登录才能访问
        map.put("/**", "user");
        return map;
    }
}
